{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(404, 430) radius:52\n",
      "(406, 442) radius:50\n",
      "(418, 435) radius:44\n",
      "(447, 429) radius:51\n",
      "(444, 421) radius:50\n",
      "(427, 413) radius:52\n",
      "(451, 405) radius:53\n",
      "(477, 410) radius:56\n",
      "(500, 409) radius:57\n",
      "(514, 395) radius:62\n",
      "(518, 378) radius:64\n",
      "(523, 371) radius:67\n",
      "(528, 364) radius:65\n",
      "(536, 362) radius:64\n",
      "(544, 350) radius:64\n",
      "(547, 345) radius:61\n",
      "(554, 350) radius:64\n",
      "(558, 349) radius:62\n",
      "(562, 345) radius:64\n",
      "(561, 341) radius:63\n",
      "(567, 351) radius:66\n",
      "(573, 345) radius:60\n",
      "(585, 338) radius:55\n",
      "(591, 324) radius:57\n",
      "(602, 318) radius:50\n",
      "(621, 315) radius:28\n",
      "(636, 328) radius:29\n",
      "(570, 333) radius:41\n",
      "(492, 335) radius:54\n",
      "(453, 333) radius:62\n",
      "(429, 329) radius:63\n",
      "(411, 327) radius:64\n",
      "(403, 328) radius:65\n",
      "(394, 328) radius:64\n",
      "(372, 330) radius:59\n",
      "(345, 328) radius:59\n",
      "(323, 331) radius:59\n",
      "(301, 328) radius:60\n",
      "(302, 328) radius:63\n",
      "(308, 328) radius:62\n",
      "(309, 334) radius:60\n",
      "(311, 341) radius:59\n",
      "(315, 354) radius:59\n",
      "(306, 374) radius:60\n",
      "(310, 396) radius:62\n",
      "(313, 439) radius:58\n",
      "(248, 478) radius:16\n",
      "(243, 477) radius:91\n",
      "(256, 478) radius:108\n",
      "(420, 212) radius:0\n",
      "(34, 478) radius:0\n",
      "(22, 470) radius:0\n",
      "(508, 367) radius:0\n",
      "(367, 267) radius:0\n",
      "(323, 235) radius:0\n",
      "(405, 412) radius:0\n",
      "(407, 405) radius:0\n",
      "(317, 231) radius:0\n",
      "(411, 410) radius:0\n",
      "(371, 250) radius:0\n",
      "(415, 422) radius:0\n",
      "(423, 420) radius:0\n",
      "(326, 250) radius:0\n",
      "(387, 247) radius:0\n",
      "(4, 438) radius:0\n",
      "(415, 239) radius:0\n",
      "(399, 254) radius:0\n",
      "(389, 246) radius:0\n",
      "(6, 438) radius:0\n",
      "(0, 424) radius:0\n",
      "(375, 264) radius:0\n",
      "(375, 264) radius:0\n",
      "(323, 235) radius:0\n",
      "(520, 479) radius:0\n",
      "(353, 231) radius:0\n",
      "(6, 436) radius:0\n",
      "(338, 378) radius:1\n",
      "(336, 384) radius:0\n",
      "(380, 370) radius:1\n",
      "(1, 416) radius:0\n",
      "(393, 366) radius:0\n",
      "(327, 227) radius:0\n",
      "(316, 192) radius:0\n",
      "(296, 169) radius:0\n",
      "(425, 240) radius:0\n"
     ]
    }
   ],
   "source": [
    "cap = cv2.VideoCapture(0)\n",
    "ret = cap.set(3, 640)\n",
    "ret = cap.set(4, 480)\n",
    "\n",
    "def camera():\n",
    "    while True:\n",
    "        ret, frame = cap.read()\n",
    "        # 转换成HSV空间\n",
    "        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)\n",
    "        # 设定绿色的阈值下限\n",
    "        low_green = np.array([65, 43, 46])\n",
    "        # 设定绿色的阈值上限\n",
    "        up_green = np.array([80, 255, 255])\n",
    "        # 设定蓝色的阈值下限\n",
    "        low_blue = np.array([108, 160, 120])\n",
    "        # 设定蓝色的阈值上限\n",
    "        up_blue = np.array([113, 200, 170])\n",
    "        # 设定红色的阈值下限\n",
    "        low_red = np.array([2, 100, 100])\n",
    "        # 设定红色的阈值上限\n",
    "        up_red = np.array([180, 145, 255])\n",
    "        \n",
    "        # 做一个颜色层\n",
    "        mask_green = cv2.inRange(hsv,low_green,up_green)\n",
    "        mask_blue = cv2.inRange(hsv,low_blue,up_blue)\n",
    "        mask_red = cv2.inRange(hsv, low_red, up_red)\n",
    "        \n",
    "        #确定小球的框框\n",
    "        contourareas_green = cv2.findContours(mask_green.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]\n",
    "        contourareas_blue = cv2.findContours(mask_blue.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]\n",
    "        contourareas_red = cv2.findContours(mask_red.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]\n",
    "        \n",
    "        \n",
    "        if len(contourareas_green)>0:\n",
    "            #从最大的框框来确定小球的内切圆\n",
    "            circle = max(contourareas_green,key=cv2.contourArea)\n",
    "            #再从内切圆里面确定小球的圆心和半径\n",
    "            ((x,y),z) = cv2.minEnclosingCircle(circle)\n",
    "            x = int(x)\n",
    "            y = int(y)\n",
    "            z = int(z)\n",
    "            print((x,y),'radius:%d' % z)\n",
    "            if z >25:\n",
    "                cv2.circle(frame,(x,y),z,(0,255,0),5)\n",
    "        \n",
    "        if len(contourareas_blue) >0:\n",
    "            circle2 = max(contourareas_blue,key=cv2.contourArea)\n",
    "            ((x2,y2),z2) = cv2.minEnclosingCircle(circle2)\n",
    "            x2 = int(x2)\n",
    "            y2 = int(y2)\n",
    "            z2 = int(z2)\n",
    "            print((x2,y2),'radius:%d' % z2)\n",
    "            if z2 >25:\n",
    "                cv2.circle(frame,(x2,y2),z2,(255,0,0),5)\n",
    "                cv2.putText(frame, 'blue', ((x2+z2),(y2+z2)), cv2.FONT_HERSHEY_PLAIN, 2, (255,0,0), 2)\n",
    "        \n",
    "        if len(contourareas_red) >0:\n",
    "            circle3 = max(contourareas_red,key=cv2.contourArea)\n",
    "            ((x3,y3),z3) = cv2.minEnclosingCircle(circle3)\n",
    "            x3 = int(x3)\n",
    "            y3 = int(y3)\n",
    "            z3 = int(z3)\n",
    "            print((x3,y3),'radius:%d' % z3)\n",
    "            if z3 >25:\n",
    "                cv2.circle(frame,(x3,y3),z3,(0,255,0),5)\n",
    "        cv2.imshow('camera',frame)\n",
    "        key = cv2.waitKey(1) & 0xff\n",
    "        if key == ord('q'):\n",
    "            break\n",
    "    cap.release()\n",
    "    cv2.destroyAllWindows()\n",
    "if __name__ == '__main__':\n",
    "    camera()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:opencv]",
   "language": "python",
   "name": "conda-env-opencv-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
